package com.htc.android.footprints.provider;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;

import com.htc.android.footprints.utils.FootprintUtils;
import com.htc.android.footprints.utils.StorageUtil;

public class FootprintContentProvider extends ContentProvider
{
  public static final String AUTGIRUTIES = "com.htc.android.footprints.provider.FootprintContentProvider";
  public static final String DATABASE_NAME = "footprints.db";
  public static final int DATABASE_VERSION = 36;
  private static final String TAG = "FootprintContentProvider";
  private Runnable copyDBToSDRunnable = new Runnable()
  {
    public void run()
    {
      Log.d("FootprintContentProvider", "db onChange sync to SD!");
      FootprintUtils.touchSDFile();
      File localFile = new File(StorageUtil.getFootprintStorageDirectory() + File.separator + "footprints.db");
      if (FootprintUtils.copyFile(new File(FootprintContentProvider.this.mDataBase.getPath()), localFile));
    }
  };
  private SQLiteDatabase mDataBase;
  private Handler mHandler;
  private HashMap<Uri, ISubProvider> mSubProviderMap = new HashMap();

  public int delete(Uri paramUri, String paramString, String[] paramArrayOfString)
  {
    ISubProvider localISubProvider = getISubProvider(paramUri);
    if (localISubProvider != null)
    {
      int i = localISubProvider.delete(paramUri, paramString, paramArrayOfString);
      getContext().getContentResolver().notifyChange(paramUri, null);
      return i;
    }
    throw new IllegalArgumentException("Unknown URL " + paramUri);
  }

  public ISubProvider getISubProvider(Uri paramUri)
  {
    String[] arrayOfString = paramUri.toString().split("/");
    if (arrayOfString.length > 3);
    for (Uri localUri = Uri.parse(arrayOfString[0] + "//" + arrayOfString[2] + "/" + arrayOfString[3]); ; localUri = paramUri)
    {
      Log.d("FootprintContentProvider", localUri.toString());
      return (ISubProvider)this.mSubProviderMap.get(localUri);
    }
  }

  public String getType(Uri paramUri)
  {
    return getISubProvider(paramUri).getType(paramUri);
  }

  public Uri insert(Uri paramUri, ContentValues paramContentValues)
  {
    ISubProvider localISubProvider = getISubProvider(paramUri);
    if (localISubProvider != null)
    {
      Uri localUri = localISubProvider.insert(paramUri, paramContentValues);
      getContext().getContentResolver().notifyChange(localUri, null);
      return localUri;
    }
    throw new IllegalArgumentException("Unknown URL " + paramUri);
  }

  public boolean onCreate()
  {
    registerSubProvider(new CategoryProvider());
    registerSubProvider(new FootprintsProvider());
    this.mDataBase = new DatabaseHelper(getContext(), "footprints.db", null, 36).getWritableDatabase();
    Iterator localIterator = this.mSubProviderMap.values().iterator();
    while (localIterator.hasNext())
      ((ISubProvider)localIterator.next()).setDB(this.mDataBase);
    this.mHandler = new Handler();
    getContext().getContentResolver().registerContentObserver(FootprintsProvider.FootprintColumns.CONTENT_URI, true, new ContentObserver(new Handler())
    {
      public void onChange(boolean paramBoolean)
      {
        if (!paramBoolean)
        {
          FootprintContentProvider.this.mHandler.removeCallbacks(FootprintContentProvider.this.copyDBToSDRunnable);
          FootprintContentProvider.this.mHandler.postDelayed(FootprintContentProvider.this.copyDBToSDRunnable, 500L);
        }
      }
    });
    if (this.mDataBase == null);
    for (int i = 0; ; i = 1)
      return i;
  }

  public Cursor query(Uri paramUri, String[] paramArrayOfString1, String paramString1, String[] paramArrayOfString2, String paramString2)
  {
    ISubProvider localISubProvider = getISubProvider(paramUri);
    if (localISubProvider != null)
    {
      Cursor localCursor = localISubProvider.query(paramUri, paramArrayOfString1, paramString1, paramArrayOfString2, paramString2);
      localCursor.setNotificationUri(getContext().getContentResolver(), paramUri);
      return localCursor;
    }
    throw new IllegalArgumentException("Unknown URL " + paramUri);
  }

  public void registerSubProvider(ISubProvider paramISubProvider)
  {
    paramISubProvider.setContext(getContext());
    this.mSubProviderMap.put(paramISubProvider.getBaseURI(), paramISubProvider);
  }

  public int update(Uri paramUri, ContentValues paramContentValues, String paramString, String[] paramArrayOfString)
  {
    ISubProvider localISubProvider = getISubProvider(paramUri);
    if (localISubProvider != null)
    {
      int i = localISubProvider.update(paramUri, paramContentValues, paramString, paramArrayOfString);
      getContext().getContentResolver().notifyChange(paramUri, null);
      return i;
    }
    throw new IllegalArgumentException("Unknown URL " + paramUri);
  }

  private class DatabaseHelper extends SQLiteOpenHelper
  {
    public DatabaseHelper(Context paramString, String paramCursorFactory, SQLiteDatabase.CursorFactory paramInt, int arg5)
    {
      super(paramCursorFactory, paramInt, i);
    }

    public void onCreate(SQLiteDatabase paramSQLiteDatabase)
    {
      Iterator localIterator = FootprintContentProvider.this.mSubProviderMap.values().iterator();
      while (localIterator.hasNext())
        ((ISubProvider)localIterator.next()).onCreate(paramSQLiteDatabase);
    }

    public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
    {
    }
  }
}

/* Location:           E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name:     com.htc.android.footprints.provider.FootprintContentProvider
 * JD-Core Version:    0.6.0
 */